package com.example.smartagriculture.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import com.example.smartagriculture.utils.Result;
import com.example.smartagriculture.service.IMeteorologyMsgService;
import com.example.smartagriculture.entity.MeteorologyMsg;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;

import org.springframework.web.bind.annotation.RestController;

/**
 * MeteorologyMsgController 前端控制器
 * @author 张芸博
 * @since 2024-12-10
 */
@RestController
@RequestMapping("/meteorology-msg")
public class MeteorologyMsgController {

    @Resource
    private IMeteorologyMsgService meteorologyMsgService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @Operation(description = "新增或更新")
    public Result save(@RequestBody MeteorologyMsg meteorologyMsg) {
        return Result.success(meteorologyMsgService.saveOrUpdate(meteorologyMsg));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @Operation(description = "通过id删除")
    public Result delete(@PathVariable Integer id) {
        MeteorologyMsg meteorologyMsg =meteorologyMsgService.getById(id);
        return Result.success(meteorologyMsgService.saveOrUpdate(meteorologyMsg));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    @Operation(description = "查询所有")
    public Result findAll() {
        QueryWrapper<MeteorologyMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<MeteorologyMsg> list=  meteorologyMsgService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @Operation(description = "根据id查询")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(meteorologyMsgService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @Operation(description = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<MeteorologyMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(meteorologyMsgService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @GetMapping("/sendMsg")
    @Operation(description = "软硬件交互")
    public Result sendMsg(String message){
        System.out.println("Controller:"+message);
        System.out.println("meteorologyMsgService:"+meteorologyMsgService);
        meteorologyMsgService.jsonToObj(message);

        return null;
    }
    @GetMapping("/getOutput")
    @Operation(description = "产量预测")
    public Result getOutput(){
        return Result.success(meteorologyMsgService.getOutput());
    }
    @GetMapping("/getOutput2")
    @Operation(description = "产量预测2")
    public Result getOutput2(){
        //return Result.success(meteorologyMsgService.getOutput());
        int sum = meteorologyMsgService.getOutput();
        //int sum = 499;
        Random rand = new Random();
        List<Integer> xData = new ArrayList<>();
        List<Integer> yData = new ArrayList<>();
        List<Integer> taskData = new ArrayList<>();
        int i = 0;
        int year = 2020;
        for (; i < 7; i++) {
            xData.add(year + i);
            yData.add(300 + rand.nextInt() % 40);
            taskData.add(400 + rand.nextInt() % 40);
        }
        xData.add(year + i);
        yData.add(300 + rand.nextInt() % 20);
        taskData.add(sum);

        HashMap<String, List<Integer>> xDataMap = new HashMap<>();
        HashMap<String, List<Integer>> yDataMap = new HashMap<>();
        HashMap<String, List<Integer>> taskDataMap = new HashMap<>();
        xDataMap.put("xData", xData);
        yDataMap.put("yData", yData);
        taskDataMap.put("taskData", taskData);
        List<HashMap<String, List<Integer>>> allData = new ArrayList<>();
        allData.add(xDataMap);
        allData.add(yDataMap);
        allData.add(taskDataMap);
        return Result.success(allData);
    }
}

